package com.ruoyi.aftersale.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.aftersale.domain.DmsRepairReservation;
import com.ruoyi.aftersale.mapper.DmsRepairReservationMapper;
import com.ruoyi.aftersale.service.IDmsRepairOrderService;
import com.ruoyi.aftersale.service.IDmsRepairReservationService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AutofillUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SerialNumberGenerator;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 维修预约Service业务层处理
 * 
 * @author liuchuanfa
 * @date 2024-03-13
 */
@Service
public class DmsRepairReservationServiceImpl extends ServiceImpl<DmsRepairReservationMapper,DmsRepairReservation> implements IDmsRepairReservationService
{
    @Autowired
    private DmsRepairReservationMapper dmsRepairReservationMapper;
    @Autowired
    private AutofillUtil autofillUtil;
    @Autowired
    private IDmsRepairOrderService iDmsRepairOrderService;
    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    SerialNumberGenerator serialNumberGenerator;

    /**
     * 查询维修预约
     * 
     * @param id 维修预约主键
     * @return 维修预约
     */
    @Override
    public DmsRepairReservation selectDmsRepairReservationById(Long id)
    {
        return dmsRepairReservationMapper.selectDmsRepairReservationById(id);
    }

    /**
     * 查询维修预约列表
     * 
     * @param dmsRepairReservation 维修预约
     * @return 维修预约
     */
    @Override
    public List<DmsRepairReservation> selectDmsRepairReservationList(DmsRepairReservation dmsRepairReservation)
    {
        return dmsRepairReservationMapper.selectDmsRepairReservationList(dmsRepairReservation);
    }

    /**
     * 新增维修预约
     * 
     * @param dmsRepairReservation 维修预约
     * @return 结果
     */
    @Override
    public int insertDmsRepairReservation(DmsRepairReservation dmsRepairReservation)
    {
        //判断创建状态，只能是预约成功
        if (dmsRepairReservation.getStates()!=1) {
            throw new ServiceException("新建预约单的状态只能是 预约成功！");
        }
        //若未填写预约单号，则后台自动生成
        if (StrUtil.isBlank(dmsRepairReservation.getReservationNo())){
            dmsRepairReservation.setReservationNo(serialNumberGenerator.generateSerialNumber("YYDH"));
        }
        //新增自动填充
        autofillUtil.autoFillCreate(dmsRepairReservation);
        //填充 经销商id和经销商名称
        dmsRepairReservation.setDealerId(SecurityUtils.getLoginUser().getUser().getDept().getParentId());
        dmsRepairReservation.setDealerName(iSysDeptService.selectDeptById(SecurityUtils.getLoginUser().getUser().getDept().getParentId()).getDeptName());
        return dmsRepairReservationMapper.insertDmsRepairReservation(dmsRepairReservation);
    }


    /**
     * 修改维修预约
     * 
     * @param dmsRepairReservation 维修预约
     * @return 结果
     */
    @Override
    public int updateDmsRepairReservation(DmsRepairReservation dmsRepairReservation)
    {
        //判断状态不可逆
        Integer states = dmsRepairReservation.getStates();
        DmsRepairReservation repairReservationDB = selectDmsRepairReservationById(dmsRepairReservation.getId());
        Integer statesDB = repairReservationDB.getStates();
        //只有未转工单的数据才可以预约失败
        if (dmsRepairReservation.getStates().equals(4) && dmsRepairReservation.getRoId()!=null){
            throw new ServiceException("已转工单，状态无法修改为预约失败！");
        }
        //如果要变成预约失败，则通过。否则只有状态逐级递增，其他情况不允许状态跨越和逆转
        if (!states.equals(4) && (states-statesDB != 1)){
            throw new ServiceException("预约状态不允许逆转 与 跨级！");
        }
        //更新自动填充
        autofillUtil.autoFillUpdate(dmsRepairReservation);
        //填充审核人、审核名称、审核时间
        dmsRepairReservation.setAuditorId(SecurityUtils.getLoginUser().getUser().getUserId());
        dmsRepairReservation.setAuditorName(SecurityUtils.getLoginUser().getUser().getUserName());
        dmsRepairReservation.setAuditorTime(new Date());
        return dmsRepairReservationMapper.updateDmsRepairReservation(dmsRepairReservation);
    }

    /**
     * 批量删除维修预约
     * 
     * @param ids 需要删除的维修预约主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairReservationByIds(Long[] ids)
    {
        return dmsRepairReservationMapper.deleteDmsRepairReservationByIds(ids);
    }

    /**
     * 删除维修预约信息
     * 
     * @param id 维修预约主键
     * @return 结果
     */
    @Override
    public int deleteDmsRepairReservationById(Long id)
    {
        return dmsRepairReservationMapper.deleteDmsRepairReservationById(id);
    }
}
